cmake_minimum_required(VERSION 3.5)

project(Hello)

set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.c)

# 设置 rpath 参数
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

if(APPLE)
    set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
elseif(UNIX)
    set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
endif()

add_library(hello_lib SHARED ${CMAKE_CURRENT_SOURCE_DIR}/src/hello.c)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)

add_executable(hello ${SOURCES})
target_link_directories(hello PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib)
target_link_libraries(hello hello_lib)

install(TARGETS hello DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
install(TARGETS hello_lib DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION
  ${CMAKE_INSTALL_PREFIX}/include)
